﻿using System.Collections.Generic;
using System.Linq;

namespace MicroDb.Generate
{
    public class BaseObj
    {
        /// <summary>
        /// 注释
        /// </summary>
        public string Comment { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 特性
        /// </summary>
        public virtual string[] Attributes { get; set; }

        /// <summary>
        /// 代码现行注释
        /// </summary>
        public Dictionary<string, string> CodeFirstXml { get; set; }

        public virtual string[] GetNameSpaces()
        {
            if (Attributes == null || Attributes.Length == 0)
            {
                return new string[0];
            }
            var list = new HashSet<string>();
            for (int i = 0; i < Attributes.Length; i++)
            {
                if (TryGetNameSpaces(Attributes[i], out string nameSpace))
                {
                    Attributes[i] = Attributes[i].Replace(nameSpace + ".", "");
                    list.Add(nameSpace);
                }
            }
            return list.ToArray();
        }

        public bool TryGetNameSpaces(string attr, out string nameSpace)
        {
            nameSpace = null;
            var lIndex = attr.LastIndexOf(".");
            if (lIndex > 0)
            {
                nameSpace = attr.Substring(0, lIndex);
                return true;
            }
            return false;
        }
    }
}
